<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 主逻辑
 * 主要是处理 onMessage onClose 三个方法
 */

use Utils\SysUtils;

require_once __DIR__.'/Config/Const.php';

function shutdown_function()
{
    $e = error_get_last();
    echo "出错啦!!!";
    print_r($e);
}

register_shutdown_function('shutdown_function');

class Events
{
    /**
     * 当客户端连上时触发
     * @param int $client_id
     */
    public static function onConnect($client_id)
    {
        $_SESSION['session_id'] = $client_id;
    }

    /**
     * 有消息时
     * @param int $client_id
     * @param string $message
     */
    public static function onMessage($client_id, $message)
    {

        // $message = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true);


        //SysUtils::sysLog($message);


        print_r($message);
        echo "<br>";
        // 获取客户端请求
        $data_arr = json_decode($message, true);

        if(!$message)
        {
            return ;
        }

        $dataDictionary = $data_arr[DATA_DICTIONARY];

        if (array_key_exists(REQUEST_ID,$data_arr)) {
            $requestId = $data_arr[REQUEST_ID];
        } else {
            $requestId = 0;
        }

        switch($data_arr[SYS_EVENT])
        {

            case 'TEST':
                new \Actions\TestAction('TEST',$requestId,$dataDictionary,$client_id);
                break;

            case 'event_test':
                new \Actions\Test('event_test',$requestId,$dataDictionary,$client_id);
                break;

        }
    }

    /**
     * 当用户断开连接时
     * @param integer $client_id 用户id
     */
    public static function onClose($client_id)
    {
        // 广播 xxx 退出了
//       GateWay::sendToAll(json_encode(array('type'=>'closed', 'id'=>$client_id)));

    }

}
